/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PostPanel.java
 *
 * Created on 22/05/2011, 19:23:39
 */

package ClientGui;

import clientData.PostData;
import clientData.PostDataInt;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author USER
 */
public class PostPanel extends javax.swing.JPanel {

    /** Creates new form PostPanel */
    public PostPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        contentEditor = new javax.swing.JTextPane();
        addPostButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        savePostButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        postList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        postContent = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        postTitle = new javax.swing.JTextPane();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        removePostButton = new javax.swing.JButton();
        authorLabel = new javax.swing.JLabel();
        lastUpdatedLabel = new javax.swing.JLabel();
        authorName = new javax.swing.JLabel();
        lastUpdated = new javax.swing.JLabel();

        jScrollPane5.setViewportView(contentEditor);

        addPostButton.setText("Add Post");
        addPostButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPostButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("title:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("content:");

        savePostButton.setText("save post");
        savePostButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savePostButtonActionPerformed(evt);
            }
        });

        postList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                postListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(postList);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(255, 51, 51));
        jLabel1.setText("Choose Post:");

        jScrollPane4.setViewportView(postContent);
        postContent.setEditable(false);

        jScrollPane2.setViewportView(postTitle);

        jLabel4.setText("Post title:");

        jLabel5.setText("Post content:");

        removePostButton.setText("remove post");
        removePostButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removePostButtonActionPerformed(evt);
            }
        });

        authorLabel.setText("Author:");

        lastUpdatedLabel.setText("Last updated:");

        authorName.setText("jLabel8");

        lastUpdated.setText("jLabel9");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(12, 12, 12)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(removePostButton, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addGap(26, 26, 26)
                            .addComponent(savePostButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(addPostButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lastUpdatedLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(authorLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lastUpdated, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(authorName, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
                            .addGap(38, 38, 38)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(savePostButton)
                            .addComponent(removePostButton))
                        .addGap(14, 14, 14)
                        .addComponent(addPostButton)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(authorLabel)
                                .addComponent(authorName))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lastUpdatedLabel)
                                .addComponent(lastUpdated))
                            .addGap(3, 3, 3))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addPostButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPostButtonActionPerformed
        if (title.getText().equals("") || contentEditor.getText().equals("")) {
            JOptionPane.showMessageDialog(new JFrame(), "You must fill in all Fields!" , "Dialog", JOptionPane.ERROR_MESSAGE);
            return;
        }
        Controller.addPost(title.getText(),contentEditor.getText());
        this.title.setText("");
        this.contentEditor.setText("");
}//GEN-LAST:event_addPostButtonActionPerformed

    private void savePostButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savePostButtonActionPerformed
        if (postTitle.getText().equals("") || postContent.getText().equals("")) {
            JOptionPane.showMessageDialog(new JFrame(), "You must fill in all Fields!" , "Dialog", JOptionPane.ERROR_MESSAGE);
            return;
        }
        Controller.editPost(postTitle.getText(), postContent.getText());
}//GEN-LAST:event_savePostButtonActionPerformed

    private void postListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_postListMouseClicked
        PostData pd = (PostData)this.postList.getSelectedValue();
        setPost(pd);
}//GEN-LAST:event_postListMouseClicked

    private void removePostButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removePostButtonActionPerformed
        Controller.removePost();
        removePostButton.setEnabled(false);
        postContent.setText("");
        postTitle.setText("");
        authorName.setText("");
        lastUpdated.setText("");
    }//GEN-LAST:event_removePostButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPostButton;
    private javax.swing.JLabel authorLabel;
    private javax.swing.JLabel authorName;
    private javax.swing.JTextPane contentEditor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lastUpdated;
    private javax.swing.JLabel lastUpdatedLabel;
    private javax.swing.JTextPane postContent;
    private javax.swing.JList postList;
    private javax.swing.JTextPane postTitle;
    private javax.swing.JButton removePostButton;
    private javax.swing.JButton savePostButton;
    private javax.swing.JTextField title;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JList getList() {
        return this.postList;
    }

    public void setPost(PostDataInt pd){
        if (pd != null){
            Controller.choosePost((PostData) pd);
            this.postContent.setText(pd.getContent());
            this.postTitle.setText(pd.getTitle());
            this.authorName.setText(pd.getCreator().getUserName());
            this.lastUpdated.setText(pd.getCreationTime().toString());
            if (pd.getCreator().getUserName().equals(Controller.getUsername()) || Controller.isAdmin() ){
                this.postContent.setEditable(true);
                this.postTitle.setEditable(true);
                this.savePostButton.setEnabled(true);
                this.removePostButton.setEnabled(true);
            } else {
                this.postContent.setEditable(false);
                this.postTitle.setEditable(false);
                this.savePostButton.setEnabled(false);
                this.removePostButton.setEnabled(false);
            }
        }
    }

    public void clearFields(){
        savePostButton.setEnabled(false);
        removePostButton.setEnabled(false);
        this.title.setText("");
        this.contentEditor.setText("");
        this.lastUpdated.setText("");
        this.authorName.setText("");
    }
}
